document.addEventListener('plusready', function() {
	var uid = plus.storage.getItem('uid');
	var merchant = plus.storage.getItem('merchant');
	var icon = plus.storage.getItem('icon');
	var nick = plus.storage.getItem('nick');
	$('#user_nick').text(nick);
	var filter_height = parseInt($('.user_box').height() + 80) + 'px';
	setTimeout(function() {
		$('#user_icon').attr('src', getAvatar(icon));
		$('.substrate').css({ 'height': filter_height, 'margin-bottom': '10px', 'background-image': 'url(' + getAvatar(icon) + ')' });
		$('.filterImg_box').css({ 'background-image': 'url(' + getAvatar(icon) + ')', 'background-size': '100% 100%', 'height': filter_height }).addClass('filterImg');
	}, 1000);

	$('#user_apply').on('tap', function() {
		$.ajax({
			type: "post",
			url: apiRoot,
			data: {
				action: 'Merchant.apply',
				uid: uid,
			},
			dataType: 'json',
			success: function(data) {
				plus.nativeUI.closeWaiting();
				if(data.error == 0) {
				console.log(JSON.stringify(data.data.data));
					setTimeout(function() {
						ws.close;
					}, 1000);
					plus.webview.create('apply_for_seller.html', 'apply_for_seller.html').show('pop-in');
				} else {
					plus.nativeUI.toast(data.desc);
					return;
				}
			},error: function(e) {
				console.log(JSON.stringify(e));
			}
		});

	})

})

window.addEventListener('changeVal', function(event){
	var nick2 = event.detail.nick;
	if(nick2) {
		nick = nick2;
		$('#user_nick').text(nick);
	}
	var icon2 = event.detail.icon;
	if(icon2) {
		icon = icon2;
		var filter_height = parseInt($('.user_box').height() + 80);
		setTimeout(function() {
			$('#user_icon').attr('src', getAvatar(icon));
			$('.substrate').css({ 'height': filter_height, 'margin-bottom': '10px', 'background-image': 'url(' + getAvatar(icon) + ')' });
			$('.filterImg_box').css({ 'background-image': 'url(' + getAvatar(icon) + ')', 'background-size': '100% 100%', 'height': filter_height }).addClass('filterImg');
		}, 500)
	}
});

var uid;
var app = angular.module('app', []);
app.controller('my_controller', function($scope, $http) {
	$scope.intview = function() {
		$http({
			method: 'post',
			url: apiRoot,
			data: {
				action: 'Merchant.checkMerchantStatus',
				uid: uid
			}
		}).then(function success(result) {
			$scope.res = result.data.data;  
		}, function errorFunction(e) {    
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(e));
		})
	}
	$scope.seller_info = function($id) {
		plus.webview.create('saler_info.html', 'saler_info.html', {}, { uid: $id }).show('pop-in');
	}

})

document.addEventListener('plusready', function() {
	ws = plus.webview.currentWebview();
	uid = plus.storage.getItem('uid');
	appElement = document.querySelector('[ng-controller=my_controller]');
	$scope = angular.element(appElement).scope();
	$scope.intview();
	$scope.$apply;   
})